package com.lr.service.impl;

import com.lr.entity.*;
import com.lr.mapper.GRMapper;
import com.lr.service.GRService;
import com.lr.util.JSONUtil;
import com.lr.util.MD5Util;
import com.lr.util.WSDJS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Service
public class GRServiceImpl implements GRService {

    @Autowired
    private GRMapper grMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${WSD}")
    private int WSD;
    @Override
    @Transactional
    public Integer Register(String userName, String password, String phone) {
        User user=grMapper.getUserByPhone(phone);
        if(user!=null){
            return 2;
        }
        try{
            user=new User();
            user.setUserName(userName);
            user.setPassword(MD5Util.getPwd(password));
            user.setPhone(phone);
            grMapper.addUser(user);
            grMapper.addResume(user.getId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    @Override
    public String login(String phone, String password,String code,HttpSession session) {
        if(!redisTemplate.opsForValue().get(session.getId()+"grcode").equals(code)){
            return "-1";
        }
        User user=grMapper.getUserByPhone(phone);
        if(user==null){
            return "0";
        }
        if(user.getPassword().equals(MD5Util.getPwd(password))){
            String token= UUID.randomUUID().toString();
            redisTemplate.opsForValue().set(token, JSONUtil.ObjectToJson(user),1800, TimeUnit.SECONDS);
            grMapper.upLogTime(user.getId());
            return token;
        }
        return "2";
    }

    @Override
    @Transactional
    public Map logInfo(String userJSON) {
        User user=JSONUtil.jsonToObject(userJSON,User.class);
        Map<String,Object> map=new HashMap<String ,Object>();
        Resume resume=grMapper.getResumeByUserId(user.getId());
        map.put("user",grMapper.getUserByPhone(user.getPhone()));
        map.put("resume",resume);
        try {
            map.put("jlwsd",WSDJS.jlwsd(resume));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Integer upPublic(String token, Integer ispublic) {
        String userJSON=(String) redisTemplate.opsForValue().get(token);
        User user=JSONUtil.jsonToObject(userJSON,User.class);
        if(ispublic==1){
            try {
                int jlwsd=WSDJS.jlwsd(grMapper.getResumeByUserId(user.getId()));
                if(jlwsd<WSD){
                    return 2;  //简历完善度不满足
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return grMapper.upPublic(user.getId(),ispublic);
    }

    @Override
    public Resume getResume(String token) {
        String userJSON=(String) redisTemplate.opsForValue().get(token);
        User user=JSONUtil.jsonToObject(userJSON,User.class);
        Resume resume=grMapper.getResumeByUserId(user.getId());
        if(redisTemplate.hasKey("Resume"+resume.getN_id())){
            redisTemplate.opsForValue().increment("Resume"+resume.getN_id());
        }else {
            redisTemplate.opsForValue().set("Resume"+resume.getN_id(),resume.getN_browseNum());
        }
        return resume;
    }

    @Override
    public Integer updateResume(Resume resume) {
        return grMapper.updateResume(resume);
    }

    @Override
    public List<Recruit> searchRecruit(Recruit recruit) {
        return grMapper.searchRecruit(recruit);
    }

    @Override
    @Transactional
    public Integer addCollect(String userJSON, String recruitIds) {
        User user=JSONUtil.jsonToObject(userJSON,User.class);
        grMapper.delCollectByTwoId(user.getId(),recruitIds);
        return grMapper.addCollect(user.getId(),recruitIds);
    }

    @Override
    @Transactional
    public Integer addRecruitResume(String userJSON, String recruitIds) {
        User user=JSONUtil.jsonToObject(userJSON,User.class);
        Resume resume=grMapper.getResumeByUserId(user.getId());
        grMapper.delRecruitResume(resume.getN_id(),recruitIds);
        grMapper.addRecruitResume(resume.getN_id(),recruitIds);
        return grMapper.addJobHuntingLog(new Date(),user.getId(),recruitIds);
    }
    @Override
    public Recruit getRecruitById(Integer id) {
        return grMapper.getRecruitById(id);
    }

    @Override
    public List<Collect> getCollect(String userJSON) {
        List<Collect> list=grMapper.getCollectByUserId(JSONUtil.jsonToObject(userJSON,User.class).getId());
        for(Collect collect:list){
            collect.setRstatus(grMapper.getStatusById(collect.getRecruitId()));
        }
        return list;
    }

    @Override
    public Integer delCollect(Integer id) {
        return grMapper.delCollect(id);
    }

    @Override
    public List<JobHuntingLog> getJobHuntingLog(String userJSON) {
        return grMapper.getJobHuntingLog(JSONUtil.jsonToObject(userJSON,User.class).getId());
    }

    @Override
    public Integer delLog(Integer id) {
        return grMapper.delLog(id);
    }


}
